﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using XQ.Framework.common;
using XQ.Framework.core;

namespace XQ.Framework.model
{
    ///////////////////////////////////////////////////////////////////////////////////////////////////

    public enum Connectors
    {
        And, Or
    }

    public enum Operators
    {
        Equal, NotEqual, Bigger, BiggerOrEqual, Smaller, SmallerOrEqual, Like, NotLike, In, NotIn, Is
    }

    public class ConditionModel
    {
        public ConditionModel()
        {
            Connector = Connectors.And;
            Operator = Operators.Equal;
        }

        /// <summary>
        /// 连接符（and or 等等）
        /// </summary>
        public Connectors Connector { get; set; }
        public string ConnectorStr()
        {
            switch (Connector)
            {
                case Connectors.And: return "and";
                case Connectors.Or: return "or";
                default: return string.Empty;
            }
        }
        /// <summary>
        /// a='b', a in ('','',''), a like '%%'中的a
        /// </summary>
        public string Key { get; set; }
        /// <summary>
        /// = or <> or > or <
        /// </summary>
        public Operators Operator { get; set; }
        public string OperatorStr()
        {
            switch (Operator)
            {
                case Operators.Equal: return "=";
                case Operators.Bigger: return ">";
                case Operators.BiggerOrEqual: return ">=";
                case Operators.Smaller: return "<";
                case Operators.SmallerOrEqual: return "<=";
                case Operators.NotEqual: return "<>";
                case Operators.Like: return "like";
                case Operators.NotLike: return "not like";
                case Operators.In: return "in";
                case Operators.NotIn: return "not in";
                case Operators.Is: return "is";
                default: return string.Empty;
            }
        }
        /// <summary>
        /// 参数值，用于多参数的in 或者 not in 中
        /// </summary>
        public List<object> Values { get; set; }

        public object Value
        {
            get
            {
                return FirstValue();
            }
            set
            {
                SetFirstValue(value);
            }
        }

        public void SetFirstValue(object obj)
        {
            if (Values == null)
            {
                Values = new List<object>();
            }
            else
            {
                Values.Clear();
            }
            Values.Add(obj);
        }

        public object FirstValue()
        {
            if (!ValidateHelper.IsPlumpList(Values))
            { return null; }
            return Values[0];
        }

        public string OrderBy { get; set; }
        public string GroupBy { get; set; }
        public string LimitMin { get; set; }
        public string LimitLen { get; set; }
    }

}
